package com.fanfanwang.ffw.app.dialog;

import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.search.core.PoiInfo;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.RemindTimeListAdapter;
import com.fanfanwang.ffw.app.adapter.RouteLineAdapter;
import com.fanfanwang.ffw.app.bean.ParkingInfoBean;
import com.fanfanwang.ffw.app.ui.home.RoutePlanDemo;
import com.fanfanwang.ffw.app.ui.my.MsgSetAct;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RouteLineDialog{
	int count = 0;
	// 存放传过来的context
	private Context context;
	// 对话框布局对象
	private View view;
	private ListView listView;
	private List<PoiInfo> list=new ArrayList<PoiInfo>();
	private RouteLineAdapter adapter;
	// 对话框对象
	private Dialog wordDialog;
	private String flag;
	private int pos;//这个表示默认那个被选中。
	private boolean isStart;//
	public RouteLineDialog(Context context,List<PoiInfo> poiList,boolean flag) {// 直接传过来声音的byte[],对话框里面显示的内容
		this.context = context;
		this.list.addAll(poiList);
		this.isStart=flag;//true表示是起始地点。false表示终点
	}
	/**
	 * @param context
	 * (in) 上下文
	 * @param word
	 * (in) 传进来的单词
	 */
	public RouteLineDialog(Context context) {// 直接传过来声音的byte[],对话框里面显示的内容
		this.context = context;
	}
	/**
	 * 弹出自动关闭的模态对话框对象
	 */
	public void init() {
		// 动态加载布局,将布局文件实例化成一个views
		view = LayoutInflater.from(context).inflate(R.layout.dialog_route_line,
				null);
		if (wordDialog != null && wordDialog.isShowing()) {
			wordDialog.dismiss();
		}
		//wordDialog = new AlertDialog.Builder(context).create();
		wordDialog=new Dialog(context);
		// 参数true指出此对话框类似于非模态对话框
		wordDialog.setCancelable(false);
		wordDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置对话框对象所使用的布局
		wordDialog.getWindow().setContentView(view);
		wordDialog.setCancelable(true);
		listView=(ListView)view.findViewById(R.id.remind_time_listView);

		adapter=new RouteLineAdapter(context, list);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long id) {

				ShutAutoDialog();
				if(isStart)
				{
					RoutePlanDemo.iGetPlace.setStartPlace(list.get(pos).name);
				}else{
					RoutePlanDemo.iGetPlace.SetEndPlace(list.get(pos).name);
				}
				//				MsgSetAct.iRemindTimeable.setRemindTime(list.get(pos),pos);
			}
		});
		try {
			wordDialog.show();
			WindowManager m = ((MsgSetAct) context).getWindowManager();
			Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
			// 以下代码设置对话框 的布局参数
			WindowManager.LayoutParams params = wordDialog.getWindow()
					.getAttributes();
			// 设置dialog的宽和高
			// 设置对话框的布局参数为居中
			params.gravity = Gravity.CENTER;
			params.width=(int)(d.getWidth()*0.9);
			params.height=(int)(d.getHeight()*0.7);
			wordDialog.onWindowAttributesChanged(params);
			wordDialog.getWindow().setAttributes(params);

		} catch (Exception e) {
		}
	}
	public void setSelect(){

	}
	/**
	 * 此方法用于关闭自动对话框,不论是人为的关闭还是自动关闭都请调用此方法
	 */
	private void ShutAutoDialog() {
		if (wordDialog != null) {
			wordDialog.dismiss();
			list=null;
		}
	}

}
